.TH std::counted_iterator::counted_iterator 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::counted_iterator::counted_iterator \- std::counted_iterator::counted_iterator

.SH Synopsis
   constexpr counted_iterator() requires                              \fB(1)\fP \fI(since C++20)\fP
   std::default_initializable<I> = default;
   constexpr counted_iterator( I x, std::iter_difference_t<I> n );    \fB(2)\fP \fI(since C++20)\fP
   template< class I2 >

       requires std::convertible_to<const I2&, I>                     \fB(3)\fP \fI(since C++20)\fP

           constexpr counted_iterator( const counted_iterator<I2>&
   other );

   Constructs a new iterator adaptor.

   1) Default constructor. Value-initializes the underlying iterator and initialized
   the underlying length with 0. Operations on the resulting iterator have defined
   behavior if and only if the corresponding operations on a value-initialized I also
   have defined behavior.
   2) The underlying iterator is initialized with std::move(x) and the underlying
   length is initialized with n. The behavior is undefined if n is negative.
   3) The underlying iterator and length are initialized with those of other.

.SH Parameters

   x     - iterator to adapt
   n     - distance to the end
   other - iterator adaptor to convert

.SH Example


// Run this code

 #include <algorithm>
 #include <initializer_list>
 #include <iostream>
 #include <iterator>

 int main()
 {
     static constexpr auto pi = {3, 1, 4, 1, 5, 9, 2};

     // (1) default constructor:
     constexpr std::counted_iterator<std::initializer_list<int>::iterator> i1{};
     static_assert(i1 == std::default_sentinel);
     static_assert(i1.count() == 0);

     // (2) initializes the iterator and length respectively:
     constexpr std::counted_iterator<std::initializer_list<int>::iterator> i2{
         pi.begin(), pi.size() - 2
     };
     static_assert(i2.count() == 5);
     static_assert(*i2 == 3 && i2[1] == 1);

     // (3) converting-constructor:
     std::counted_iterator<std::initializer_list<const int>::iterator> i3{i2};

     std::ranges::copy(i3, std::default_sentinel,
         std::ostream_iterator<const int>{std::cout, " "});
 }

.SH Output:

 3 1 4 1 5

.SH See also

   operator= assigns another iterator adaptor
   (C++20)   \fI(public member function)\fP
